﻿using System.Threading.Tasks;
using Xproject.Framework.Core;
using Xproject.Framework.Core.DataContext;
using Xproject.Framework.Core.Repositories;
using Model.Admin;
using Repository.WXProjectAdmin;
using Xproject.Util;
using Model.WXshopBuiness;
using System;
using System.Collections.Generic;
using Model.Admin.DTO;
using System.Linq.Expressions;
using System.Linq;
using Model.Util;
namespace Services.WXProjectAdmin
{
    public class SystemAdServices:Service
    {
       IRepositoryAsync<Advertisement> m_AdvertisementRepository;     
        IRepositoryAsync<UploadFile> m_UploadFileRepository;
        public SystemAdServices(IDataContextAsync dataContext) : base(dataContext)
        {
            m_AdvertisementRepository = m_UnitOfWork.RepositoryAsync<Advertisement>();
            m_UploadFileRepository = m_UnitOfWork.RepositoryAsync<UploadFile>();
        }


        public bool EditAdvertisement(UploadFile up, Advertisement advert, string userid)
        {
            int result = 0;
            m_UnitOfWork.BeginTransaction();
            if (advert != null)
            {
                advert.EntityState = EntityState.Modified;
                advert.CreateTime = DateTime.Now;
                m_AdvertisementRepository.Update(advert);
                result++;
            }
            if (up != null)
            {
                up.CreateTime = DateTime.Now;
                up.Creator = userid;
                up.EntityState = EntityState.Added;
                m_UploadFileRepository.Insert(up);
                result++;
            }
            if (m_UnitOfWork.SaveChanges() == result)
            {
                m_UnitOfWork.Commit();
                Cache_Advertisements.ClearAd_QDSYLB();
                return true;
            }
            else
            {
                m_UnitOfWork.Rollback();
                return false;
            }
        }

        public bool AddAdvertisement(UploadFile up, Advertisement advert,string userid)
        {
            int result = 0;
            m_UnitOfWork.BeginTransaction();
            if (advert != null)
            {
                advert.EntityState = EntityState.Added;
                advert.CreateTime = DateTime.Now;
                m_AdvertisementRepository.Insert(advert);
                result++;
            }
            if (up != null)
            {
                up.CreateTime = DateTime.Now;
                up.Creator = userid;
                up.EntityState = EntityState.Added;
                m_UploadFileRepository.Insert(up);
                result++;
            }
            if (m_UnitOfWork.SaveChanges() == result)
            {
                m_UnitOfWork.Commit();
                Cache_Advertisements.ClearAd_QDSYLB();
                return true;
            }
            else
            {
                m_UnitOfWork.Rollback();
                return false;
            }
        }

        public bool DelAdvertisement(Advertisement advert)
        {
            if (advert != null)
            {
                advert.EntityState = EntityState.Deleted;
                m_AdvertisementRepository.Delete(advert);
                return m_UnitOfWork.SaveChanges() == 1;
            }
            return false;
        }
        private List<Advertisement> GetAllAdvertisements(string site)
        {
            if (Cache_Advertisements.Ad_QDSYLB == null)
            {
                Cache_Advertisements.Ad_QDSYLB =  m_AdvertisementRepository.GetAllAdvertisements(site);
            }
            return Cache_Advertisements.Ad_QDSYLB;
        }


        public Advertisement GetById(string site,string Id)
        {
            return m_AdvertisementRepository.Get(site, Id);
        }

        public List<Advertisement> GetAllAdvertisements(string site, int limit, int offset, string adName, string keyWord, string adType, string remarks,
            DateTime? startTime, DateTime? endTime, string order, string ordername, out int total)
        {
            List<Advertisement> allads = GetAllAdvertisements(site);
            if (!string.IsNullOrEmpty(adName))
            {
                allads = allads.Where(i => i.AdName.Contains(adName)).ToList();
            }
            if (!string.IsNullOrEmpty(keyWord))
            {
                allads = allads.Where(i => i.KeyWord.Contains(keyWord)).ToList();
            }
            if (!string.IsNullOrEmpty(adType))
            {
                allads = allads.Where(i => i.AdType.Contains(adType)).ToList();
            }
            if (!string.IsNullOrEmpty(remarks))
            {
                allads = allads.Where(i => i.Remarks.Contains(remarks)).ToList();
            }
            if (startTime.HasValue)
            {
                allads = allads.Where(i => i.CreateTime>=startTime.Value).ToList();
            }
            if (endTime.HasValue)
            {
                allads = allads.Where(i => i.CreateTime <= endTime.Value).ToList();
            }            
            total = allads.Count;
            return allads.Skip(offset).Take(limit).ToList();
        }

        public List<Advertisement> GetTopNAdvertisement(string site, int topN, string type)
        {
            List<Advertisement> allads =  GetAllAdvertisements(site);
            List<Advertisement> list = allads.Where(i => i.Site == site && i.AdType == type).ToList();            
            return list.Take(topN).ToList();
        }
}
}
